<?php
    class SysconfigsController extends AppController
    {
        var $name='Sysconfigs';
        var $uses=array();
        var $layout='admin';
        var $helpers=array('Javascript');
        var $components = array('Auth');
        var $pageTitle='系统配置';
      function beforeFilter()
      {  
        $this->userScope = array('admin_leve'=>1);
        $this->authorize = 'controller';
        $this->Auth->loginError = '未能登录成功,密码或帐号错误';
        $this->Auth->authError='还未登录或者你没有管理员权限';
    }
    
    function isAuthorized()
    {
           if($this->Auth->user('admin_leve')>0)
            return true;
        else
            return false;
    }
    
    function admin_index()
    {
        if(empty($this->data)){
            $this->data['rooturl'] = Configure::read('rootUrl');
            $this->data['baseurl']=Configure::read('App.baseUrl'); 
            $this->data['fmtime'] = Configure::read('flashMaxTime');
            $this->data['tpldebug'] =Configure::read('tplDebug');
            $this->set('rooturl',$this->data['rooturl']);
            $this->set('baseurl',$this->data['baseurl']);
            $this->set('fmtime',$this->data['fmtime']);
            $this->set('tpldebug',$this->data['tpldebug']);
        }else{
                $rplstr="<?php \r\n";
                $rplstr.='Configure::write("rootUrl","'.$this->data['rooturl']."\");\r\n";
                $rplstr.='Configure::write("App.baseUrl","'.$this->data['baseurl']."\");\r\n";
                $rplstr.='Configure::write("tplDebug","'.$this->data['tpldebug']."\");\r\n";
                $rplstr.='Configure::write("flashMaxTime","'.$this->data['fmtime']."\");\r\n";
               $rplstr.=" ?>";
               if(!file_put_contents('../config/yongscore.php',$rplstr)){
                    $this->flash('写入文件时出错，修改核心配置未成功','/admin/sysconfigs',5);
                    return;
                }
                /*暂时关闭数据库密码修改功能
                if(!empty($this->data['dbpw'])){
                    if($this->data['dbpw']!=$this->data['dbpw2']){
                        $this->flash('两次输入的数据库密码不一致!','/admin/sysconfigs',5);
                        return;
                    }
                    $db = file_get_contents('../config/db.php');
                    $rplstr="yongs_dbpw_begin\r\n";
                    $rplstr.="'password'=>'{$this->data['dbpw']}',\r\n";
                    $rplstr.='yongs_dbpw_end';
                    preg_replace('/yongs_dbpw_begin.+yongs_dbpw_end/',$rplstr,$db);
                    if(!file_put_contents('../config/db.php',$db)){
                        $this->flash('数据库密码未能修改成功','/admin/sysconfigs',5);
                        return;
                    }
                }
                */
                $this->flash('配置修改成功','/admin/sysconfigs',5);
           }
        }
        
        //生成首页
        function admin_createIndexPage()
        {
            $page=$this->requestAction("/html/index");
            $path='../../index.html';
            if(file_put_contents($path,$page))
                $this->flash('生成首页成功','-1',5);
             else
                $this->flash('未能成功生成首页','-1',5);
        }
    }
?>